<template>
    <new-search-panel :showAdvanced="false" @search="onSearch" @reset="onReset">
        <template #default>
            <a-col :md="24" :sm="24">
                <a-form-item label="角色名">
                    <a-input :maxLength="35" v-model:value="queryParam.name" allow-clear placeholder="请输入角色名" />
                </a-form-item>
            </a-col>
            <a-col :md="24" :sm="24">
                <a-form-item label="唯一编号">
                    <a-input :maxLength="35" v-model:value="queryParam.code" allow-clear placeholder="请输入唯一编号" />
                </a-form-item>
            </a-col>
        </template>
        <template #button>
            <a-button type="primary" @click="addItem()">
                <template #icon><PlusOutlined /></template>
                新增</a-button
            >
        </template>
    </new-search-panel>
    <dynamic-table
        :scrollX="1000"
        :fixed="true"
        :tableType="1"
        ref="tableRef"
        :columns="columns"
        :get-list-func="getRolePage"
        :queryParam="queryParam"
        rowKey="id"
    >
        <template #title>
            <a-button type="primary" @click="addItem"> 新增字典 </a-button>
            <a-button :disabled="isDisabled" type="primary" @click="deleteItems"> 删除 </a-button>
        </template>
        <template #action="{ record }">
            <a @click="editItem(record)">编辑</a>
            <a-divider type="vertical" />
            <a-dropdown>
                <a class="ant-dropdown-link"> 更多 <DownOutlined /> </a>
                <template #overlay>
                    <a-menu>
                        <a-menu-item>
                            <a @click="$refs.roleMenu.init(record)">授权菜单</a>
                        </a-menu-item>
                        <a-menu-item>
                            <a @click="$refs.roleDataScope.init(record)">授权数据</a>
                        </a-menu-item>
                        <a-menu-item>
                            <a-popconfirm placement="topRight" title="确认删除？" @confirm="deleteItem(record)">
                                <a>删除</a>
                            </a-popconfirm>
                        </a-menu-item>
                    </a-menu>
                </template>
            </a-dropdown>
        </template>
    </dynamic-table>
    <roleDataScope ref="roleDataScope" @refresh="refresh" />
    <roleMenu ref="roleMenu" @refresh="refresh" />
</template>
<script lang="ts">
import { defineComponent, reactive, toRefs, createVNode, computed, ref } from 'vue'
import { message, Modal } from 'ant-design-vue'
import { QuestionCircleOutlined } from '@ant-design/icons-vue'
import { DynamicTable, newSearchPanel } from '@/components'
import { getRolePage, sysRoleAdd, sysRoleEdit, sysRoleDelete } from '@/api/system/role'
import { getFormSchema } from './form-schema'
import { columns } from './columns'
import { hasPermission } from '@/utils/permission/hasPermission'
import { useFormModal } from '@/hooks/useFormModal'
import roleDataScope from './components/roleDataScope.vue'
import roleMenu from './components/roleMenu.vue'

export default defineComponent({
    name: 'SystemRole',
    components: {
        DynamicTable,
        newSearchPanel,
        roleDataScope,
        roleMenu
    },
    setup() {
        const tableRef = ref<any>(null)

        const state = reactive({
            tableLoading: false,
            /** 查询 */
            queryParam: {
                name: '',
                code: ''
            },
            /** 多选 */
            rowSelection: {
                onChange: (selectedRowKeys, selectedRows) => {
                    state.rowSelection.selectedRowKeys = selectedRowKeys
                },
                selectedRowKeys: []
            }
        })

        /* 删除多项 **/
        const deleteItems = () => {
            Modal.confirm({
                title: '提示',
                icon: createVNode(QuestionCircleOutlined),
                content: '您确定要删除所有选中吗？',
                onOk: async () => {
                    await sysRoleDelete(state.rowSelection.selectedRowKeys.toString())
                    tableRef.value.refreshTableData()
                    state.rowSelection.selectedRowKeys = []
                }
            })
        }

        /**
         * 删除
         * @param record
         */
        function deleteItem(record) {
            sysRoleDelete(record).then(() => {
                message.success('删除成功')
                tableRef.value.refreshTableData()
            })
        }

        /**
         * 查询
         */
        function onSearch() {
            tableRef.value.queryParam = state.queryParam
            tableRef.value.resetPagination()
            tableRef.value.refreshTableData()
        }
        /**
         * 查询当前页
         */
        const refresh = () => {
            tableRef.value.refreshTableData()
        }
        /**
         * 重置
         */
        function onReset() {
            state.queryParam = {
                name: '',
                code: ''
            }
            onSearch()
        }
        /* 添加 **/
        const addItem = () => {
            useFormModal({
                title: '新增角色',
                formSchema: getFormSchema(),
                handleOk: async (modelRef, state) => {
                    await sysRoleAdd(modelRef)
                    tableRef.value.refreshTableData()
                }
            })
        }

        /* 编辑 **/
        const editItem = (record) => {
            useFormModal({
                title: '编辑角色',
                formSchema: getFormSchema(),
                fields: record,
                handleOk: async (modelRef, state) => {
                    delete modelRef.dataScopeType
                    await sysRoleEdit(modelRef)
                    tableRef.value.refreshTableData()
                }
            })
        }
        const isDisabled = computed(() => state.rowSelection.selectedRowKeys.length == 0)

        return {
            ...toRefs(state),
            columns,
            tableRef,
            getRolePage,
            isDisabled,
            addItem,
            editItem,
            deleteItems,
            deleteItem,
            onSearch,
            refresh,
            onReset
        }
    }
})
</script>
